package com.example.im.ui.custom

import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.Drawable
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Handler
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.View

import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatTextView

import com.example.im.R
import com.example.im.utils.LogUtils

import java.io.IOException


/**
 * Incremental change is better than ambitious failure.
 *
 * @author : [MysticCoder](http://mysticcoder.coding.me)
 * @date : 2018/3/21
 * @desc : 不用时一定要调用{[.release]}
 */


class VoiceTextView(context: Context, attrs: AttributeSet) : AppCompatTextView(context, attrs) {

    private var mediaPlayer: MediaPlayer? = null

    val isPlaying: Boolean
        get() = mediaPlayer!!.isPlaying

    init {
        mediaPlayer = MediaPlayer()

        mediaPlayer!!.setOnPreparedListener { mp ->
            startAnim()
            mp.seekTo(0)
            mp.start()
        }
        mediaPlayer!!.setOnErrorListener { mp, _, _ ->
            mp.reset()
            false
        }
        mediaPlayer!!.setOnCompletionListener { stopAnim() }

    }

    /**
     * 开始播放
     */
    fun startPlay(path: String) {
        if (mediaPlayer!!.isPlaying) {
            mediaPlayer!!.pause()
            stopAnim()
        } else {
            mediaPlayer!!.reset()
            mediaPlayer!!.setDataSource(path)
            mediaPlayer!!.prepare()
        }
    }

    /**
     * 结束播放
     */
    fun stopPlay() {
        if (mediaPlayer!!.isPlaying) {
            mediaPlayer!!.pause()
            stopAnim()
        }
    }

    /**
     * 开始动画
     */
    fun startAnim() {
        for (drawable in compoundDrawables) if (drawable != null) {
            val animation = drawable as AnimationDrawable
            animation.start()
            break
        }
    }

    /**
     * 结束动画
     */
    fun stopAnim() {
        for (drawable in compoundDrawables) if (drawable != null) {
            val animation = drawable as AnimationDrawable
            animation.selectDrawable(0)
            animation.stop()
            break
        }
    }

    /**
     * 设置 drawableLeft
     *
     * @param
     * @param id
     */
    fun setDrawableLeft(@DrawableRes id: Int) {
        val left = resources.getDrawable(id)
        left.setBounds(0, 0, left.minimumWidth, left.minimumHeight)
        setCompoundDrawables(left, null, null, null)
    }

    fun setDrawableRight(@DrawableRes id: Int) {
        val right = resources.getDrawable(id)
        right.setBounds(0, 0, right.minimumWidth, right.minimumHeight)
        setCompoundDrawables(null, null, right, null)
    }

    /**
     * 释放资源
     */
    fun release() {
        if (mediaPlayer != null) {
            mediaPlayer!!.stop()
            mediaPlayer!!.release()
            mediaPlayer = null
        }
    }

}
